package com.woniu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.pojo.Package;
import com.woniu.pojo.PackageItemContact;
import com.woniu.vo.SearchCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.woniu.pojo.PackageImagesContact;
import com.woniu.service.IPackageImagesContactService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务包与图片关联 前端控制器
 * </p>
 *
 * @author YMY
 * @since 2022-10-12
 */
@Api(tags = "服务包与图片关联")
@RestController
@RequestMapping("/package-images-contact")
    public class PackageImagesContactController extends BaseController{

        @Autowired
        private IPackageImagesContactService packageImagesContactService;

        @ApiOperation("查找所有服务包与图片的关联")
        @GetMapping
        public Result queryAll(){
            List<PackageImagesContact> packageImagesContactList = packageImagesContactService.list();

            return toDataResult(packageImagesContactList) ;

        }

        @ApiOperation("为服务包新增图片")
        @PostMapping
        public Result addImagesByPackage(@RequestBody PackageImagesContact contact){

            return toResult(packageImagesContactService.save(contact));
        }

        @ApiOperation("根据服务包id查找对应图片")
        @GetMapping("/{packageId}")
        public Result queryByPackageId(@PathVariable Integer packageId){
            Map<String,Object> map=new HashMap<>();
            map.put("package_id",packageId.toString());
            List<PackageImagesContact> list = packageImagesContactService.listByMap(map);
            list.sort((p1,p2)->p1.getIndexs()-p2.getIndexs());
            return toDataResult(list);
        }

        @ApiOperation("删除服务包对应图片或全部图片")
        @DeleteMapping
        public Result deleteByPackageId(@RequestParam Integer packageId,@RequestParam(required = false) String url){
            Map<String,Object> map=new HashMap<>();
            map.put("package_id",packageId.toString());
            if(url!=null&&url.length()>0)map.put("images",url);
            return toResult(packageImagesContactService.removeByMap(map));
        }



//        @ApiOperation("分页按条件查找所有服务包与图片的关联")
//        @PostMapping("/search/{page}/{size}")
//        public Result search(@PathVariable int page, @PathVariable int size, @RequestBody SearchCriteria searchCriteria){
//            IPage<PackageImagesContact> p=new Page<PackageImagesContact>(page,size);
//            QueryWrapper<PackageImagesContact> wrapper=new QueryWrapper<>();
//            if(searchCriteria.getName()!=null)wrapper.like("name",searchCriteria.getName());
//            if(searchCriteria.getStartTime()!=null)wrapper.gt("create_time",searchCriteria.getStartTime());
//            if(searchCriteria.getEndTime()!=null)wrapper.lt("create_time",searchCriteria.getEndTime());
//            if(searchCriteria.getOther()!=null&&searchCriteria.getOther().size()>0){
//                searchCriteria.getOther().forEach((key,value)->{
//                wrapper.eq(key,value);
//                });
//            }
//            if(searchCriteria.getOtherLike()!=null&&searchCriteria.getOtherLike().size()>0){
//                searchCriteria.getOtherLike().forEach((key,value)->{
//                wrapper.like(key,value);
//                });
//            }
//
//            p = packageImagesContactService.page(p, wrapper);
//            PageResult pageResult=new PageResult();
//            pageResult.setData(p.getRecords());
//            pageResult.setTotal(p.getTotal());
//
//            return toDataResult(pageResult);
//        }





        }

